package com.zhiqian.community.controller;

import com.zhiqian.community.entity.User;
import com.zhiqian.community.service.LikeService;
import com.zhiqian.community.util.CommunityUtil;
import com.zhiqian.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LikeController {
    @Autowired
    private LikeService likeService;
    @Autowired
    private HostHolder hostHolder;

    @PostMapping("/doLike")
    @ResponseBody
    public String doLike(Integer entityId,int entityType,Integer entityUserId){
        if (entityId==null||entityType==0){
            return CommunityUtil.getJSONString(1,"点赞失败!");
        }
        User user = hostHolder.getUser();
        likeService.like(user.getId(),entityType,entityId,entityUserId);
        Long likeCount = likeService.queryEntityLikeCount(entityType, entityId);
        int likeStatus = likeService.queryEntityLikeStatus(user.getId(), entityType, entityId);
        Map<String, Object> result = new HashMap<>(2);
        result.put("likeCount",likeCount);
        result.put("likeStatus",likeStatus);
        return CommunityUtil.getJSONString(0,"点赞成功！",result);
    }
}
